package top.smallway.lightbandassistant_app.method;

import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.Log;

import java.io.IOException;


public class AudioPlayer {
    private static final String TAG = "AudioPlayer";
    private MediaPlayer mediaPlayer;
    private AudioPlayerListener listener;

    public interface AudioPlayerListener {
        void onLoadingStarted();

        void onLoadingComplete();

        void onPlayingStarted();

        void onPlayingStopped();
    }

    public void setAudioPlayerListener(AudioPlayerListener listener) {
        this.listener = listener;
    }

    public void playAudio(String audioPath) {
        stopAudio(); // 先停止当前正在播放的音频

        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioAttributes(getAudioAttributes()); // 设置音频属性
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (listener != null) {
                        listener.onLoadingComplete(); // 加载完成回调
                    }
                    mediaPlayer.start();
                    if (listener != null) {
                        listener.onPlayingStarted(); // 开始播放回调
                    }
                    Log.d(TAG, "Audio playback started");
                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopAudio(); // 音频播放完成后停止
                    if (listener != null) {
                        listener.onPlayingStopped(); // 停止播放回调
                    }
                }
            });

            if (listener != null) {
                listener.onLoadingStarted(); // 加载开始回调
            }

            // 根据音频文件路径判断是播放本地文件还是网络资源
            if (audioPath.startsWith("http://") || audioPath.startsWith("https://")) {
                mediaPlayer.setDataSource(audioPath); // 播放网络资源
            } else {
                mediaPlayer.setDataSource(audioPath); // 播放本地文件
            }

            mediaPlayer.prepareAsync(); // 异步加载音频
        } catch (IOException e) {
            Log.e(TAG, "Failed to play audio: " + e.getMessage());
        }
    }

    public void stopAudio() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
            if (listener != null) {
                listener.onPlayingStopped(); // 停止播放回调
            }
            Log.d(TAG, "Audio playback stopped");
        }
    }

    public void reset() {
        if (mediaPlayer != null) {
            mediaPlayer.reset();
        }

    }

    private AudioAttributes getAudioAttributes() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
        } else {
            return new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build();
        }
    }
}
